from google.appengine.ext import db

class Major(db.Model):
  short_name = db.StringProperty()
  long_name = db.StringProperty()
  def __str__(self):
    return self.long_name
    
class MajorNames(db.Model):
  """Class used to represent all possible alternative names to majors"""
  name = db.StringProperty()
  major = db.ReferenceProperty(Major)

class Classes(db.Model):
  major = db.ReferenceProperty(Major)
  short_name = db.StringProperty()
  long_name = db.StringProperty()
  # TODO: Uncomment
  # Used to sort classes liek R5B correctly
  #class_num = db.IntegerProper()
  def __str__(self):
    return self.major.short_name + self.short_name
    
class Section(db.Model):
  # Refers to the class this section belongs to
  classes = db.ReferenceProperty(Classes)
  # LEC, DIS, etc...
  type = db.StringProperty()
  # section number. Eg. 001, 102, 4-6, etc
  num = db.StringProperty()
  # refers to the primary session, usually Discussion -> Lecture
  primary_lec = db.SelfReferenceProperty()
  # time. eg MWF1-3
  time = db.StringProperty()
  location = db.StringProperty()
  instructor = db.StringProperty()
  #ccn = db.IntegerProperty()
  ccn = db.StringProperty()
  last_updated = db.StringProperty()
  #books_url = db.LinkProperty()
  books_url = db.StringProperty()
  #primary = db.BooleanProperty()
  primary = db.StringProperty()
  def __str__(self):
    return '%s - %s %s' % (self.classes, self.type, self.num)  

class UserInfo(db.Model):
  """Keyed by user.user_id"""
  first_name = db.StringProperty()
  last_name = db.StringProperty()
  profile = db.TextProperty()
  user = db.UserProperty(auto_current_user_add=True)
  berkeley_email = db.EmailProperty(required=True)
  major = db.ReferenceProperty(Major)
  grad = db.IntegerProperty()
  classes = db.ListProperty(db.Key)  # List of Lecture's

class Item(db.Model):
  TYPES = set((
    'Homework',
    'Exam',
    'Text',
    'Quiz',
    'Project',
    'Midterm',
    'Final',
    'Reading',
    'Other'
  ))
  item_type = db.StringProperty(choices=TYPES)
  title = db.StringProperty()
  section = db.ReferenceProperty()
  due = db.DateProperty()
  created = db.DateTimeProperty(auto_now_add=True)
  created_by = db.UserProperty(auto_current_user_add=True)
  last_editor = db.UserProperty(auto_current_user_add=True)
  #created_by = db.ReferenceProperty(UserInfo)
  resource = db.TextProperty()
  def getComments(self):
    return Comment.all().filter('item = ', self)
  
class Comment(db.Model):
  content = db.TextProperty()
  datetime = db.DateTimeProperty(auto_now_add=True)
  created_by = db.UserProperty(auto_current_user_add=True)
  item = db.ReferenceProperty(Item)
  thumbs_up_count = db.IntegerProperty(default=0)
  thumbs_down_count = db.IntegerProperty(default=0)

class ThumbAction(db.Model):
  comment = db.ReferenceProperty(Comment)
  user = db.UserProperty()
  action = db.StringProperty(choices=set(('UP','DOWN')))

class Greeting(db.Model):
  author = db.UserProperty()
  content = db.StringProperty(multiline=True)
  file = db.BlobProperty()
  date = db.DateTimeProperty(auto_now_add=True)